﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;

namespace DrawingShips
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Timer tickTimer;
        SimpleShip testShip = new SimpleShip();

        const double DEGREES_TO_RADIANS = 2 * Math.PI / 360;

        public MainWindow()
        {
            InitializeComponent();

            tickTimer = new Timer();
            tickTimer.AutoReset = true;
            tickTimer.Interval = 30;
            tickTimer.Elapsed += new ElapsedEventHandler(tickTimer_Elapsed);

            tickTimer.Enabled = true;
            
        }

        void tickTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                Dispatcher.BeginInvoke((Action)(() => tickTimer_Elapsed(sender, e)));
            }
            else
            {

                #region Input

                if (Keyboard.IsKeyDown(Key.Left))
                {
                    testShip.DeltaRotation-= .2;
                }
                if (Keyboard.IsKeyDown(Key.Right))
                {
                    testShip.DeltaRotation += .2;
                }
                if (Keyboard.IsKeyDown(Key.Up))
                {
                    testShip.Thrust += 1;
                }
                else
                {
                    testShip.Thrust = 0;
                }

                #endregion

                #region Physics Tick

                testShip.Rotation += testShip.DeltaRotation;

                testShip.YVelocity -= Math.Cos(DEGREES_TO_RADIANS * testShip.Rotation) * (testShip.Thrust / testShip.Mass);
                testShip.XVelocity -= Math.Sin(DEGREES_TO_RADIANS * testShip.Rotation) * (testShip.Thrust / testShip.Mass);

                testShip.XPosition += testShip.XVelocity;
                testShip.YPosition += testShip.YVelocity;

                #endregion

                #region Draw

                Canvas MainCanvas = new Canvas();
                this.Content = MainCanvas;
                MainCanvas.Background = Brushes.Black;

                Image shipImage = testShip.shipImage;
                MainCanvas.Children.Add(new Line() { X1 = 0, X2 = 100, Y1 = 0, Y2 = 100 });
                MainCanvas.Children.Add(shipImage);
                Canvas.SetLeft(shipImage, testShip.XPosition);
                Canvas.SetTop(shipImage, testShip.YPosition);

                #endregion
            }
        }
    }
}
